package com.vincent.kdp;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;


public abstract class BaseFragment extends Fragment {

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Lg("onCreate");
        getBundleData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Lg("onCreateView");
        View view = inflater.inflate(layoutId(), container, false);
        ButterKnife.bind(this,view);
        findViewById(view);
        initViewData();
        listener();
        return view;
    }

    /**
     * get contentView resourceId
     *
     * @return
     */
    protected abstract int layoutId();

    /**
     * bundle,used to translate data between fragments
     */
    protected abstract void getBundleData();

    /**
     * get view by id
     *
     * @param v
     */
    protected abstract void findViewById(View v);

    /**
     * init component data
     */
    protected abstract void initViewData();

    /**
     * listen click event
     */
    protected abstract void listener();


    /**
     * add new fragment
     */
    public void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.fragment_right_in, R.anim.fragment_left_out, R.anim.fragment_left_in, R.anim.fragment_right_out);
        fragmentTransaction.replace(R.id.content, fragment, fragment.getClass().getSimpleName());
        Log.d("BaseFragment", "fragment tag =" + fragment.getClass().getSimpleName());
        fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
        fragmentTransaction.commitAllowingStateLoss();
    }

    /**
     * return back
     */
    public void popBackStack() {
        int backStackCount = getFragmentManager().getBackStackEntryCount();
        if (backStackCount > 0) {
            if (backStackCount == 1) {
                getActivity().finish();
                hideSoftInputWindow();
            } else {
                getFragmentManager().popBackStack();
                hideSoftInputWindow();
            }
        }
    }

    /**
     * 打印日志
     *
     * @param message
     */
    public void Lg(String message) {
//        VLog.d(getClass().getSimpleName(), message);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public void onResume() {
//        presenter.attach((V) this);
        super.onResume();
//		MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
    }

    public void onPause() {
        Lg("onPause");
        super.onPause();
//		MobclickAgent.onPageEnd(getClass().getSimpleName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
    }

    private void hideSoftInputWindow() {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();//isOpen若返回true，则表示输入法打开
        if (isOpen && getActivity().getCurrentFocus() != null) {
            //这里有个问题没有解决，就是imm.isActive()有的时候明明输入法没有打开也返回true，然后进入这里之后，getCurrentFocus()
            // ==null报错，所以这里在if语句里面添加了getCurrentFocus()！=null的判断。但是这里的问题还是没有解决
            ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
